﻿Imports System.Collections.ObjectModel
Imports WPFSearchDataGrid_DAL


Namespace Views

    Public Class ViewModel

        Private m_context As AdventureWorksEntities
        Private m_businessentityaddresses As ObservableCollection(Of BusinessEntityAddress)
        Private m_addresses As ObservableCollection(Of Address)
        Private m_filter As Func(Of BusinessEntityAddress, Boolean) = Nothing

        Public Property BusinessEntityAddresses As ObservableCollection(Of BusinessEntityAddress)
            Get
                If IsNothing(m_filter) Then
                    Return m_businessentityaddresses
                Else
                    Dim bea = m_context.BusinessEntityAddresses.Where(m_filter)
                    m_businessentityaddresses = New ObservableCollection(Of BusinessEntityAddress)(bea)
                    Return m_businessentityaddresses
                End If
            End Get
            Set(value As ObservableCollection(Of BusinessEntityAddress))
                m_businessentityaddresses = value
            End Set
        End Property

        Public Property Addresses As ObservableCollection(Of Address)
            Get
                Return m_addresses
            End Get
            Set(value As ObservableCollection(Of Address))
                m_addresses = value
            End Set
        End Property

        Public Sub Filter(func As Func(Of BusinessEntityAddress, Boolean))
            m_filter = func
        End Sub


        Public Sub New()
            m_context = New AdventureWorksEntities()
            Dim bea = m_context.BusinessEntityAddresses.Where(Function(e) e.BusinessEntityID < 50)
            m_businessentityaddresses = New ObservableCollection(Of BusinessEntityAddress)(bea)
            Dim addr = From b In bea Join a In m_context.Addresses On b.AddressID Equals a.AddressID Select a
            m_addresses = New ObservableCollection(Of Address)(addr)
        End Sub

    End Class

End Namespace